export const _import = (file, path) => eval(
  process.env.NODE_ENV === "production" ?
    require(`@/${path || 'views'}/${file}`) :
    require(`@/${path || 'views'}/${file}`).default)


/**
 * 随机字符
 * @param length
 * @param character
 * @returns {string}
 */
export function randomChar(length, character) {
  character = character || '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  let result = '';
  for (let i = length; i > 0; --i)
    result += character[Math.floor(Math.random() * character.length)];
  return result;
}

/**
 * 随机数字
 * @param min
 * @param max
 * @returns {*}
 */
function getRandomNumber(min, max) {
  return Math.floor(Math.random() * (max - min)) + min;
}


/**
 * 数据克隆
 * <span>本方法只支持数据克隆，不支持方法克隆</span>
 * <span>lodash.cloneDeep支持克隆方法</span>
 * @param object
 * @returns Object
 */
export function cloneDeep(object) {
  return JSON.parse(JSON.stringify(object))
}

/**
 * 基于异步的线程等待
 * @param ms
 * @returns {Promise<unknown>}
 */
export function sleep(ms) {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve()
    }, ms)
  })
}

/**
 * 将一个列表数据转化为Tree数据
 * @param listData 要转换的列表结构数据
 * @param idField
 * @param parentField
 * @param childrenField
 * @returns {Array}
 */
export function listConvertTree(listData, idField = 'id', parentField = 'parentId', childrenField = 'children') {
  let treeData = [], pidGroupMap = []
  for (let i = 0, l = listData.length; i < l; i++) {
    pidGroupMap[listData[i][idField]] = listData[i]
  }
  for (let i = 0, l = listData.length; i < l; i++) {
    if (pidGroupMap[listData[i][parentField]] && listData[i][idField] !== listData[i][parentField]) {
      if (!pidGroupMap[listData[i][parentField]][childrenField]) {
        pidGroupMap[listData[i][parentField]][childrenField] = []
      }
      pidGroupMap[listData[i][parentField]][childrenField].push(listData[i])
    } else {
      treeData.push(listData[i])
    }
  }
  return treeData
}

/**
 * 将一个Tree数据转化为列表数据
 * @param treeData 要转换的树结构数据
 * @param listData 保存结果的列表结构数据，初始传[]
 * @param parentVal 当前遍历节点的父级节点值
 * @param idField
 * @param parentField
 * @param childrenField
 */
export function treeConvertListDF(treeData, listData = [], parentVal = null, idField = 'id', parentField = 'parentId', childrenField = 'children') {
  for (let entryNode of treeData) {
    let deconstruct = {...entryNode}
    listData.push(deconstruct)
    if (entryNode[childrenField]) {
      treeConvertListDF(entryNode[childrenField], listData, entryNode[idField], idField, parentField, childrenField)
    }
    delete deconstruct[childrenField]
    deconstruct[parentField] = parentVal
  }
  return listData
}


/**
 * 格式化String的占位符
 * @param template
 * @param context
 * @returns {*}
 */
export function stringFormat(template, context) {

  return template.replace(/\{\{(.*?)\}\}/g, (match, key) => context[key.trim()]);
}

/**
 * 替换url中的参数值
 * @param tagUrl
 * @param paramName
 * @param replaceValue
 * @returns {*}
 */
export function replaceParamVal(tagUrl, paramName, replaceValue) {
  return tagUrl.replace(eval('/(' + paramName + '=)([^&]*)/gi'), paramName + '=' + replaceValue);
}